import mainMenu from './mainMenu.vue';
import IndexAdvertising from './IndexAdvertising.vue';
import goodsType from './goodsType.vue';
import goods from './goods.vue';
import goodsList from './goodsList.vue';
import preSaleList from './preSaleList.vue';
import distributionList from './distributionList.vue';
import sortButton from './sortButton.vue';
import rightSidedBar from './rightSidedBar.vue';
import goodsPic from './goodsPic.vue';
import shoppingCartGoodsList from './shoppingCartGoodsList.vue';
import bottomOperate from './bottomOperate.vue';
import { numberFormat, clearQueryParams, getUrlVal } from './utils'

const numberPriceDisplay = (v) => {
  return v ? numberFormat(v, { unit: '' }) : '无价格'
}
const numberAllPriceDisplay = (v) => {
  return numberFormat(v, { unit: '' })
}
const numberDisplay = (v) => {
  return numberFormat(v, { currency: '', decimal: 0, unit: '' })
}
const clearParams = ({ data, el = [], callback } = {}) => {
  return clearParams(data, el = [], callback)
}
const formatData = ({ data, el = [], callback, configProperties = {} } = {}) => {
  const { params, path, body } = configProperties;
  const formData = clearQueryParams(data, el = [], callback)
  let classifiedData = {}
  if (params || path || body) {
    classifiedData = Object.keys(formData).reduce((acc, key) => {
      if (params && params.includes(key)) {
        acc.params = acc.params || {};
        acc.params[key] = formData[key];
      } else if (path && path.includes(key)) {
        acc.path = acc.path || {};
        acc.path[key] = formData[key];
      } else if (body && body.includes(key)) {
        acc.body = acc.body || {};
        acc.body[key] = formData[key];
      }
      return acc;
    }, {})
  }
  return Object.keys(classifiedData).length ? classifiedData : formData
}
const getQueryParams = (name) => {
  return getUrlVal(name)
}
export default {
  components: [
    mainMenu,
    IndexAdvertising,
    goodsType,
    goods,
    goodsList,
    preSaleList,
    distributionList,
    sortButton,
    rightSidedBar,
    goodsPic,
    shoppingCartGoodsList,
    bottomOperate
  ],
  displays: [
    { name: 'formatData', fun: formatData },
    { name: 'numberPriceDisplay', fun: numberPriceDisplay },
    { name: 'numberAllPriceDisplay', fun: numberAllPriceDisplay },
    { name: 'numberDisplay', fun: numberDisplay },
    { name: 'getQueryParams', fun: getQueryParams }
  ],
  install (app, options) {
    this.components.forEach((component) => {
      app.component(component.name, component);
    })
    this.displays.forEach((display) => {
      app.config.globalProperties[`$${display.name}`] = display.fun;
    })
  }
}